﻿using System;
using System.Text.RegularExpressions;
using EduFormManager.Models;

namespace EduFormManager.Utils
{
    public static class TagHelper
    {
        public const string FEDERAL_EDU_FORMS_TAG = "EduForms";
        public const string ARCHIVE_FEDERAL_EDU_FORMS_TAG = "ArchiveEduForms";
        public const string ADDON_EDU_FORMS_TAG = "AddonEduForms";
        public const string ARCHIVE_ADDON_EDU_FORMS_TAG = "ArchiveAddonEduForms";
        public const string FEDERAL_MUNIT_FORMS_TAG = "MunitForms";
        public const string ARCHIVE_FEDERAL_MUNIT_FORMS_TAG = "ArchiveMunitForms";
        public const string QUESTIONARY_TAG = "Questionary";

        public enum TagType
        {
            Tile,
            Document,
            Container
        }
        public static String GetFormDataTag(TagType type, FormData fd)
        {
            switch (type)
            {
                case TagType.Tile:
                    return String.Format("tile-data-{0}-form_type-{1}", fd.Id, fd.Form.Type.Id);
                case TagType.Document:
                    return String.Format("doc-data-{0}-form_type-{1}", fd.Id, fd.Form.Type.Id);
                case TagType.Container:
                    return String.Format("container-data-{0}-form_type-{1}", fd.Id, fd.Form.Type.Id);
                default:
                    return null;
            }
        }

        public static String GetFormContainerTag(EduOrg edu, AllformType.Type.Types type, bool isArchive)
        {
            return String.Format("{2}container-edu-{0}-form_type-{1}", edu.Id, (int)type,isArchive ? "archive" : "");
        }
        public static String GetFormContainerTag(Municipality m, AllformType.Type.Types type, bool isArchive)
        {
            return String.Format("{2}container-munit-{0}-form_type-{1}", m.Id, (int)type, isArchive ? "archive" : "");
        }
        public static String GetFormContainerTag(EduOrg edu, AllformType type, bool isArchive)
        {
            return String.Format("{2}container-edu-{0}-form_type-{1}", edu.Id, type.Id, isArchive ? "archive" : "");
        }
        public static String GetFormContainerTag(Municipality m, AllformType type, bool isArchive)
        {
            return String.Format("{2}container-munit-{0}-form_type-{1}", m.Id, type.Id, isArchive ? "archive" : "");
        }

        public static String GetTag(TagType type, Entity obj, string addon = "")
        {
            string tag = String.Format("{0}-{1}-{2}", (obj is EduOrg ? "edu" : obj is Municipality ? "munit" : obj is EduOrgKind ? "edu_type" : "ent"), obj.Id, addon);
            switch (type)
            {
                case TagType.Tile:
                    return "tile-" + tag;
                case TagType.Document:
                    return "doc-" + tag;
                case TagType.Container:
                    return "container-" + tag;
                default:
                    return null;
            }
        }

        public static int? GetFormDataId(string tag)
        {
            Regex dataIdPattern = new Regex(@"data-(?<num>\d+)");
            Match dataIdMatch = dataIdPattern.Match(tag);
            if (dataIdMatch.Success)
            {
                int dataId = -1;
                bool isParsed = Int32.TryParse(dataIdMatch.Groups["num"].Value, out dataId);
                return !isParsed ? null : new int?(dataId);
            }
            return null;
        }

        public static string GetQuestionaryTag(Questionary q, TagType tagType)
        {
            switch (tagType)
            {
                case TagType.Tile:
                    return String.Format("tile-questionary-{0}", q.Id);
                case TagType.Container:
                    return String.Format("container-questionary-{0}", q.Id);
                case TagType.Document:
                    return String.Format("document-questionary-{0}", q.Id);
                default:
                    return null;
            }
        }

        public static int? GetQuestionaryId(string tag)
        {
            Regex idPattern = new Regex(@"questionary-(?<num>\d+)");
            Match idMatch = idPattern.Match(tag);
            if (idMatch.Success)
            {
                int id = -1;
                bool isParsed = Int32.TryParse(idMatch.Groups["num"].Value, out id);
                return !isParsed ? null : new int?(id);
            }
            return null;
        }
    }
}
